/* eslint valid-jsdoc: "off" */

"use strict";

/**
 * @param {Egg.EggAppInfo} appInfo app info
 */
module.exports = (appInfo) => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {});

  // 引入Op模块
  const Op = require('sequelize').Op;

  config.cluster = {
    listen: {
      path: "",
      port: 8009,
      hostname: "0.0.0.0",
    },
  };
  config.uploadDir = "app/public/avatar/upload";
  config.multipart = {
    // fileSize: '50mb',  // 文件大小
    fileModeMatch: /^(\/tools\/excel\/import)$/, // '/tools/excel/import'接口使用file模式，其他使用stream模式
    fileExtensions: [".xlsx", ".xls", "docx", "doc", "txt"],
  };

  // 在config/config.default.js的module.exports里面添加
  // 跨域配置
  config.cors = {
    origin: "*", // 表示允许的源
    allowMethods: "GET,HEAD,PUT,POST,DELETE,PATCH", // 表示允许的http请求方式
  };

  // 配置公共的api
  config.api = "https://movie.douban.com/";

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + "_1629354829255_7414";

  // add your middleware config here
  config.middleware = ["tokenHandler"];
  // 中间件执行匹配开启
  config.tokenHandler = {
    match(ctx) {
      // 只匹配指定路由，反之如果只忽略指定路由，可以用ignore
      //匹配不需要验证token的路由
      const url = ctx.request.url;
      const whiteList = ["/user/login", "/user/register"];
      if (whiteList.includes(url)) {
        // ctx.logger.info('config.tokenHandler:','关闭token验证')
        return false;
      } else {
        // ctx.logger.info('config.tokenHandler:','开启token验证')
        return true; // 开启中间件，开启token验证
      }
    },
  };

  // 给pforbidip中间件传入的参数
  config.forbidip = {
    ip: ["192.168.0.10"],
  };

  // add your user config here
  const userConfig = {
    // myAppName: 'egg',
  };

  // 1.117.235.17
  config.sequelize = {
    dialect: "mysql", // 表示使用mysql
    host: "db", // 连接的数据库主机地址
    port: 3306, // mysql服务端口
    database: "chrome-server", // 数据库名
    username: "root", // 你的数据库用户名
    password: "123456", // 你的数据库密码
    define: {
      timestamps: false, // 默认添加createdAt,updatedAt,deletedAt时间戳
      paranoid: true, // 设置软删除，删除时不删除数据，而是通过更新deleteAt标识删除
      freezeTableName: true, // 冻结表名，防止建表时表名修改为复数形式
      underscored: false, // 防止驼峰式字段被转为下划线
    },
    // 这里有坑，sequelize用的是UTC时间，我们在东八区，所以要加8
    // https://stackoverflow.com/questions/47367893/sequelize-reads-datetime-in-utc-only
    dialectOptions: {
      dateStrings: true,
      typeCast(field, next) {
        if (field.type === 'DATETIME') {
          return field.string();
        }
        return next();
      },
    },
    timezone:'+08:00',//https://blog.csdn.net/luxiahongyi/article/details/123621628
    // timezone: "+8:00",
    // 使用默认运算符别名
    operatorsAliases: {
      $eq: Op.eq,
      $ne: Op.ne,
      $gte: Op.gte,
      $gt: Op.gt,
      $lte: Op.lte,
      $lt: Op.lt,
      $not: Op.not,
      $in: Op.in,
      $notIn: Op.notIn,
      $is: Op.is,
      $like: Op.like,
      $notLike: Op.notLike,
      $iLike: Op.iLike,
      $notILike: Op.notILike,
      $regexp: Op.regexp,
      $notRegexp: Op.notRegexp,
      $iRegexp: Op.iRegexp,
      $notIRegexp: Op.notIRegexp,
      $between: Op.between,
      $notBetween: Op.notBetween,
      $overlap: Op.overlap,
      $contains: Op.contains,
      $contained: Op.contained,
      $adjacent: Op.adjacent,
      $strictLeft: Op.strictLeft,
      $strictRight: Op.strictRight,
      $noExtendRight: Op.noExtendRight,
      $noExtendLeft: Op.noExtendLeft,
      $and: Op.and,
      $or: Op.or,
      $any: Op.any,
      $all: Op.all,
      $values: Op.values,
      $col: Op.col
    },
  };

  config.redis = {
    client: {
      port: 6379,
      host: "redis",
      password: "123456",
      db: 1,
    },
  };

  config.view = {
    mapping: {
      ".html": "ejs",
    },
  };

  config.jwt = {
    secret: "123456",
  };

  config.security = {
    csrf: {
      enable: false,
    },
  };

  return {
    ...config,
    ...userConfig,
  };
};

exports.io = {
  namespace: {
    "/": {
      // connectionMiddleware是在client保持连接的时候调用的中间件
      connectionMiddleware: ["auth"],
      // packetMiddleware是在server发送包给client之后调用的中间件
      packetMiddleware: [],
    },
  },
};